package com.hemerasolutions.v2trainer.component.detail;


import java.util.List;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseGroupDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ModalitySearchCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.table.GroupedExerciseTableCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseGroupDetailDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ModalitySearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;

@Component("exerciseGroupDetails")
@Scope("prototype")
public class ExerciseGroupDetailsComp extends BaseComponent implements ExerciseGroupDetailsCompInterface, ModalitySearchDelegateInterface {

	private static final long serialVersionUID = 1L;

	private ExerciseGroupDetailDelegateInterface delegate;
	
	private VersionableExerciseGroup versionableExerciseGroup;
	
	private ExerciseGroup exerciseGroup;
	
	private Exercise exercise; 

	@Autowired
	private ExerciseDetailsCompInterface exerciseDetails;
	
	@Autowired
	private GroupedExerciseTableCompInterface exerciseGroupTable;
	
	@Autowired
	private ModalitySearchCompInterface modalitySearch;
	
	
	@Override
	public void refresh(){
		exerciseGroupTable.setExerciseGroup(this.versionableExerciseGroup.getExerciseGroup());
		exerciseGroupTable.refresh();
		
	}
	
	
	
	@Override
	public void toSearchModality(){
		this.modalitySearch.setDelegate(this);
		this.modalitySearch.refresh();
	}

	public void changeEgModality(){
		this.refresh();
	}	
	
	@Override
	public void toViewExercise(){
		this.exerciseDetails.setExercise(this.exercise);
		this.exerciseDetails.refresh();
	}

	@Override
	public ExerciseGroup getExerciseGroup() {
		return exerciseGroup;
	}

	@Override
	public void setExerciseGroup(ExerciseGroup exerciseGroup) {
		this.exerciseGroup = exerciseGroup;
	}

	@Override
	public ExerciseDetailsCompInterface getExerciseDetails() {
		return exerciseDetails;
	}

	@Override
	public void setExerciseDetails(ExerciseDetailsCompInterface exerciseDetails) {
		this.exerciseDetails = exerciseDetails;
	}

	@Override
	public Exercise getExercise() {
		return exercise;
	}

	@Override
	public void setExercise(Exercise exercise) {
		this.exercise = exercise;
	}
	
	
	@Override
	public ExerciseGroupDetailDelegateInterface getDelegate() {
		return delegate;
	}

	@Override
	public void setDelegate(ExerciseGroupDetailDelegateInterface delegate) {
		this.delegate = delegate;
	}

	@Override
	public VersionableExerciseGroup getVersionableExerciseGroup() {
		return versionableExerciseGroup;
	}

	@Override
	public void setVersionableExerciseGroup(
			VersionableExerciseGroup versionableExerciseGroup) {
		this.versionableExerciseGroup = versionableExerciseGroup;
	}

	@Override
	public void saveExerciseGroup(ActionEvent ev){
		if(this.delegate != null){
			this.delegate.onSaveVersionableExerciseGroup(this.versionableExerciseGroup,ev);
		}
	}
	
	@Override
	public void createExerciseGroup(ActionEvent ev){
		if(this.delegate != null){
			this.delegate.onCreateVersionableExerciseGroup(this.versionableExerciseGroup,ev);
		}
	}

	@Override
	public void cancelChange(){
		this.delegate.onCancelChange(this.versionableExerciseGroup);
	}

	@Override
	public GroupedExerciseTableCompInterface getExerciseGroupTable() {
		return exerciseGroupTable;
	}

	@Override
	public void setExerciseGroupTable(
			GroupedExerciseTableCompInterface exerciseGroupTable) {
		this.exerciseGroupTable = exerciseGroupTable;
	}


	@Override
	public boolean onVersionableModalitySelected(
			VersionableModality versionableModality) {

		this.exerciseGroup.setTrainingType(versionableModality.getModality().getTrainingType());
		this.exerciseGroup.setModality(versionableModality.getModality());
		this.exerciseGroup.getExercises().clear();
		
		return false;
	}


	@Override
	public void onVersionableModalitySearchCompleted(
			List<VersionableModality> versionableModalitys) {
		//Nothing
	}


	public ModalitySearchCompInterface getModalitySearch() {
		return modalitySearch;
	}


	public void setModalitySearch(ModalitySearchCompInterface modalitySearch) {
		this.modalitySearch = modalitySearch;
	}	
	
	
	
	
}
